import request from '../../api'
import { dataFormat } from '@/utils/data.ts'

const userSign = {
  namespaced: true,
  state: {
    // 日历数据
    calendarData: [],
    // 是否已经打卡
    isClocked: '',
    // 排行榜消息
    rankMessage: []
  },
  mutations: {
    getSignInfo(state: any, payload: any) {
      state.calendarData = payload.data
      state.isClocked = state.calendarData[dataFormat()]
    },
    getRank(state: any, payload: any) {
      let arr = payload.data
      state.rankMessage = []
      for (var key in arr) {
        state.rankMessage.push({
          name: arr[key].userName,
          clockNum: arr[key].signAmount
        })
      }
    }
  },
  actions: {
    // 获取用户签到情况
    getSignInfo({ commit }: any, payload: any) {
      request.userSign.signInfo(payload.data).then((res: any) => {
        commit('getSignInfo', { data: res.data.dateInfo })
      })
    },
    // 获取排行榜信息
    getRank({ commit }: any) {
      request.userSign.rank().then((res: any) => {
        commit('getRank', { data: res.data })
      })
    }
  }
}

export default userSign
